<template>
  <div class="user">
    <pageSearch
      :searchFormConfig="searchFormConfig"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
    />
    <pageTalbe
      :tableConfig="tableConfig"
      pageName="users"
      ref="pageTableRef"
      @newBtnClick="handleNewData"
      @editBtnClick="handleDditData"
    />
    <pageModal
      :modalConfig="modalConfigRef"
      pageName="users"
      ref="pageModalRef"
      :defaultInfo="defaultInfo"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from 'vuex'

import { searchFormConfig } from './config/search.config'
import { tableConfig } from './config/table.config'
import { modalConfig } from './config/modal.config'

import pageSearch from '@/components/page-search'
import pageTalbe from '@/components/page-table'
import pageModal from '@/components/page-modal'

import { usePageSearch } from '@/hook/page-table-hook'
import { usePageModal } from '@/hook/page-modal-hook'

export default defineComponent({
  name: 'user',
  components: {
    pageSearch,
    pageTalbe,
    pageModal
  },

  setup() {
    const newCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = false
    }
    const editCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = true
    }
    // 2.动态添加部门和角色列表
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find(
        (item) => item.field === 'departmentId'
      )
      departmentItem!.options = store.state.entireDepartment.map(
        (item: any) => {
          return { title: item.name, value: item.id }
        }
      )
      const roleItem = modalConfig.formItems.find(
        (item) => item.field === 'roleId'
      )
      roleItem!.options = store.state.entireDepartment.map((item: any) => {
        return { title: item.name, value: item.id }
      })
      return modalConfig
    })
    const [handleSearch, pageTableRef, handleReset] = usePageSearch()
    const [pageModalRef, defaultInfo, handleNewData, handleDditData] =
      usePageModal(newCallback, editCallback)

    return {
      searchFormConfig,
      tableConfig,
      handleSearch,
      pageTableRef,
      handleReset,
      handleNewData,
      handleDditData,
      pageModalRef,
      modalConfigRef,
      defaultInfo
    }
  }
})
</script>

<style scoped lang="less">
.header {
  color: blue;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 0 0;
}
</style>
